package com.youlu.campus.web.common.base.delay;

import com.youlu.campus.service.base.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.concurrent.TimeUnit;

/**
 * @author zgz
 * @date 2020/8/18 19:27
 */
@Slf4j
public abstract class DelayHandleBaseService extends BaseService {

    public void exe(DelayQueueRequest request) {
        log.info("DelayHandleBaseService exe request {}", request);
        String lock = "delay:queue:md5:" + DigestUtils.md5Hex(request.toString());
        if (lockService.tryLock(lock, 10L, TimeUnit.SECONDS)) {
            try {
                this.run(request.getContent());
            } finally {
                lockService.unLock(lock);
            }
        }
    }

    protected abstract void run(String content);
}
